package com.poisonh.unifiedauth.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 展示名片信息Dialog
 * Created by wish30_4 on 2017/3/28.
 */

public class UserCardDialogFragment extends BaseDialogFragment {




    private TextView car_name;
    private TextView cellphone;
    private TextView company_name;
    private TextView email_text;
    private ImageView head_img;


    private String strcar_name;
    private String mStrcellphone;
    private String Strcompany_name;
    private String mStremail_text;
    private String mStrUID;
    private String mStrheadumg;
    private String mStrCard;
    private Button button_add,button_cancel;



    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        car_name = (TextView) view.findViewById(R.id.car_name);//名字
        cellphone= (TextView) view.findViewById(R.id.cellphone);//手机号码
        company_name= (TextView) view.findViewById(R.id.company_name);//公司名称
        email_text= (TextView) view.findViewById(R.id.email_text);//emeil
        head_img= (ImageView) view.findViewById(R.id.head_img);//头像
        button_add= (Button) view.findViewById(R.id.button_add);//添加名片夹按钮
        button_cancel= (Button) view.findViewById(R.id.button_cancel);//取消按钮
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                QueryCardInfo();
            }
        });
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        if (strcar_name!=null)
        {
            car_name.setText(strcar_name);//名字
        }
        if (mStrcellphone!=null)
    {
        cellphone.setText(mStrcellphone);//手机号码

    }if (Strcompany_name!=null)
    {
        company_name.setText(Strcompany_name);//公司名称
    }if (mStremail_text!=null)
    {
        email_text.setText(mStremail_text);//email
    }if (mStrheadumg!=null)
    {

      //  head_img.setText(mStrheadumg);
        Glide.with(this).load(ConstantUitls.REQUEDT_IMG_URL+mStrheadumg).into(head_img);
    }

//        if (strcar_name!=null)
//    {
//        car_name.setText(strcar_name);
//    }if (strcar_name!=null)
//    {
//        car_name.setText(strcar_name);
//    }

    }
    @Override
    public View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        @SuppressLint("InflateParams")
        View view = inflater.inflate(R.layout.layout_dialog_usercard, null);

        Strcompany_name = getArguments().getString("COMPANY");//公司名称
        mStremail_text = getArguments().getString("EMAIL");//email
        mStrheadumg = getArguments().getString("HEADIMG");//头像
        mStrCard = getArguments().getString("ID");//id
        mStrcellphone = getArguments().getString("MOBILE");//手机号
        strcar_name = getArguments().getString("NAME");//姓名昵称
        mStrUID = getArguments().getString("UID");//uid


      //  mStrUserToken = getArguments().getString("token");
      //  mStrUserId = getArguments().getString("scanning_userId");




        return view;
    }
    /**
     * 扫描二维码获取用户名片信息
     */
    public   void QueryCardInfo()
    {

        HttpParams params = new HttpParams();
        params.put("token", mStrUserToken);
        //扫描用户id
        params.put("userId",mStrUserId);
        //被扫描用户id
        params.put("destId", mStrUID);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.CARDUSER, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                try
                {
                    JSONObject object = new JSONObject(response);
                    String msg=object.getString("msg");
                    String code=object.getString("code");
                    if("200".equals(code)){
                        Toast.makeText(getActivity(),"添加成功",Toast.LENGTH_SHORT).show();
                        dismiss();
                    }else {
                        Toast.makeText(getActivity(),"添加失败",Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
                Log.d("AAA", response);
            }

            @Override
            public void onFailure(String failReason)
            {
                Log.d("AAA", failReason);
                Toast.makeText(getActivity(),"该用户已添加到名片夹",Toast.LENGTH_LONG).show();
            }
        });
    }


}
